//
//  EnumView.swift
//  swiftStudy
//
//  Created by ext.zhangjunfei on 2021/7/1.
//

import SwiftUI

struct EnumView: View {
    
    let code_1:String
    let code_2:String
    
    init() {
        code_1 =
            """
            //定义枚举
            enum State{
                case loading,suc,err,empty
            }

            //使用枚举
            var state:State = State.empty

            print(state)

            //如果变量已经确认为某个枚举类型，赋值可以这样写
            state = .loading
            """
        
        code_2 =
            """
            //为枚举关联一个值
            enum State2:String{
                case loading = "加载中"
                case suc = "成功"
                case err = "数据错误"
                case empty = "无数据"
            }

            var state_2 = State2.loading

            print(state_2.rawValue)
            """
    }
    
    var body: some View {
        ScrollView {
            LazyVStack(alignment:.leading, spacing: 10) {
                
                Section(header: SectionHeaderView(title: "枚举")) {
                    CommonTextView(content: "Swift语言中使用enmu关键字来进行枚举的创建")
                    CodeView(code: code_1).padding(10)
                }
                
                Section(header: SectionHeaderView(title: "枚举的原始值和相关值")) {
                    CommonTextView(content: "枚举的原始值特性可以将枚举值与另一种数据类型进行绑定，相关值则可以为枚举关联一些其他数据。")
                    CommonTextView(content: "Swift语言中的枚举支持声明一个原始类型，并将某个已存在的类型的值与枚举值进行绑定，示例如下：")
                    CodeView(code: code_2).padding(10)
                    
                }
            }
        }
    }
}

struct EnumView_Previews: PreviewProvider {
    static var previews: some View {
        EnumView()
    }
}
